<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;

class Cases extends Controller
{
    protected $middleware = [
        'CheckAdmin'
    ];

    public function lists(Request $request)
    {
        if ($request->isAjax()) {
            $title = $request->get('title', '', 'trim');
            $page = $request->get('page',1,'intval');
            $page = $page > 0 ? $page : 1;
            $limit = $request->get('limit',10,'intval');
            $case = Db('case');
            if ($title !== '') {
                $case->where('title|company', 'like', '%' . $title . '%');
            }
            $count = $case->count();
            $list = $case
                ->page($page,$limit)
                ->order('sort desc')
                ->select();
            return ['code' => 0, 'data' => $list, 'count' => $count];
        } else {
            return $this->fetch();
        }
    }

    public function addCase(Request $request)
    {
        if ($request->isPost()) {
            $data = [];
            $data['title'] = $request->post('title', '', 'trim');
            $data['image'] = $request->post('case_image', '', 'trim');
            $data['description'] = $request->post('description', '', 'trim');
            $data['content'] = $request->post('content', '', 'trim');
            $data['company'] = $request->post('company', '', 'trim');
            $data['sort'] = $request->post('sort', 0, 'intval');
            $data['create_time'] = date('Y-m-d H:i:s');
            if ($data['title'] == '') {
                return ['code' => 1, 'msg' => '请填写文章名称'];
            }
            if (false === Db('case')->insert($data)) {
                return ['code' => 1, 'msg' => '操作失败'];
            } else {
                return ['code' => 0, 'msg' => '操作成功'];
            }
        } else {
            return $this->fetch();
        }
    }

    public function editCase(Request $request)
    {
        if ($request->isPost()) {
            $data = [];
            $case_id = $request->post('case_id', 0, 'intval');
            $data['title'] = $request->post('title', '', 'trim');
            $data['image'] = $request->post('case_image', '', 'trim');
            $data['description'] = $request->post('description', '', 'trim');
            $data['content'] = $request->post('content', '', 'trim');
            $data['company'] = $request->post('company', '', 'trim');
            $data['sort'] = $request->post('sort', 0, 'intval');
            if ($data['title'] == '') {
                return ['code' => 1, 'msg' => '请填写案例名称'];
            }
            if (false === Db('case')->where('case_id',$case_id)->update($data)) {
                return ['code' => 1, 'msg' => '操作失败'];
            } else {
                return ['code' => 0, 'msg' => '操作成功'];
            }
        } else {
            $case_id = $request->get('id', 0, 'intval');
            $info = Db('case')->where('case_id',$case_id)->find();
            $this->assign('info', $info);
            return $this->fetch();
        }
    }

    public function deleteCase(Request $request)
    {
        $case_id = $request->get('id', 0, 'intval');
        if (false === Db('case')->where('case_id', $case_id)->delete()) {
            return ['code' => 1, 'msg' => '操作失败'];
        } else {
            return ['code' => 0, 'msg' => '操作成功'];
        }
    }
}